<?php

class Kon_Berita extends CI_Controller {

	function __construct()
	{
		parent:: __construct();
		$this->load->helper('bantuan_helper');
		$this->load->model('Konten_model');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		elseif($this->session->userdata('user_rule')=='Sales'):
			redirect('backend/no_acces');
		endif;

	}

	function index()
	{
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/kon_berita/index';
		$config['total_rows'] = count($this->Konten_model->getKonten('berita','all',FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Konten Berita';
		$data['template'] = 'kon_berita/index';
		$data['res'] = $this->Konten_model->getKonten('berita','all',FALSE,$config['per_page'],$offset);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}

	function add()
	{
		$this->form_validation->set_rules('judul', 'Judul', 'required');
		$this->form_validation->set_rules('isi_konten', 'Isi Konten', 'required');
		$this->form_validation->set_rules('published', 'Publikasi', 'required');

		if ($this->form_validation->run() == FALSE)
		{
			$data['title'] = 'Konten Berita';
			$data['template'] = 'kon_berita/add';
			$this->load->view('backend/index',$data);
		}
		else
		{
			$date = date("Y-m-d H:i:s");
			$data = array('judul'=>$this->input->post('judul'),
						  'isi_konten'=>$this->input->post('isi_konten'),
						  'page'=>'berita',
						  'date'=>$date,
						  'published'=>$this->input->post('published')
						 );
			
			$this->Konten_model->addKonten($data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Satu data berhasil ditambahkan</div>');
			redirect('backend/kon_berita');
		}
	}

	function edit()
	{
		$this->form_validation->set_rules('judul', 'Judul', 'required');
		$this->form_validation->set_rules('isi_konten', 'Isi Konten', 'required');
		$this->form_validation->set_rules('published', 'Publikasi', 'required');

		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['title'] = 'Konten Berita';
			$data['template'] = 'kon_berita/edit';
			$data['row'] = $this->Konten_model->getKonten('berita',FALSE,$id,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else
		{
			$id = $this->uri->segment(4);
			$date = date("Y-m-d H:i:s");
			$data = array('judul'=>$this->input->post('judul'),
						  'isi_konten'=>$this->input->post('isi_konten'),
						  'page'=>'berita',
						  'date'=>$date,
						  'published'=>$this->input->post('published')
						 );
			$this->Konten_model->editKonten($id,$data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil diupdate</div>');
			redirect('backend/kon_berita/edit/'.$id);
		}
	}


	function delete()
	{
		$id = $this->uri->segment(4);
		$this->Konten_model->deleteKonten($id);
		$uri = $this->uri->segment(5);
		$count = count($this->Konten_model->getKonten('berita','all',FALSE,FALSE,FALSE));
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil dihapus</div>');
		getPage('kon_berita','delete',$uri,$count);
	}
	
	function publish()
	{
		$id = $this->uri->segment(5);
		$status = $this->uri->segment(4);
		
		if ($status == 'y'):
			$data = array('published'=>'Ya');
			$this->Konten_model->editKonten($id,$data);
			$uri = $this->uri->segment(6);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil di publish</div>');
			getPage('kon_berita','edit',$uri,FALSE);
		elseif($status == 'n'):
			$data = array('published'=>'Tidak');
			$this->Konten_model->editKonten($id,$data);
			$uri = $this->uri->segment(6);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil di unpublish</div>');
			getPage('kon_berita','edit',$uri,FALSE);
		endif;
		
	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["konten_id"] as $id):
			$this->db->where('konten_id', $id);
			$this->db->delete('tb_konten');
		endforeach;
	}


}
/* Location: ./system/application/controllers/backend/content.php */
